package common.platformclient;

import com.alibaba.fastjson.JSONObject;
import feign.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Collection;
import java.util.Map;


public class PlatformClientService {

    @Autowired
    PlatformClient platformClient;

    public JSONObject test() {
        return platformClient.test01();
    }

    public JSONObject fileUpload(MultipartFile file) {
        return platformClient.fileUpload(file);
    }

    public InputStream fileDownload() throws Exception {
        Response response = platformClient.fileDownload();
        Map<String, Collection<String>> headers = response.headers();
        Collection<String> disposition = headers.get("Content-Disposition");
        disposition.stream().forEach(s -> {
            if (s.contains("fileName")) {
                String fileName = s.substring(s.indexOf("=") + 1, s.indexOf("."));
                String suffix = s.substring(s.indexOf("."));
                try {
                    fileName = URLDecoder.decode(fileName, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                System.out.println(suffix);
                System.out.println(fileName);
            }
        });
        return response.body().asInputStream();
    }
}
